//
//  PhotoListViewController.m
//  XPhoto
//
//  Created by baboy on 2019/8/9.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "PhotoListViewController.h"

@interface PhotoListViewController ()

@end

@implementation PhotoListViewController

- (BKCommentView *)commentView{
    if (!_commentView) {
        _commentView = [BKCommentView view];
        _commentView.delegate = self;
    }
    return _commentView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (PhotoTableViewCell *)photoCell{
    static NSString *CELL_ID = @"PhotoCell";
    PhotoTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CELL_ID];
    if (!cell) {
        cell = loadViewFromNib([PhotoTableViewCell class], self);
        cell.delegate = self;
    }
    return cell;
}
#pragma PhotoCell Delegate
- (void)photoTableViewCellDidClickedSerialButton:(id)photoCell{
    Post *post = [photoCell post];
    SerialPostDetailViewController *vc = [[SerialPostDetailViewController alloc] init];
    vc.post = post;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)photoTableViewCellDidClickedAddrButton:(id)photoCell{
    Post *post = [photoCell post];
    
}
- (void)photoTableViewCellDidClickedLikeButton:(id)photoCell{
    Post *post = [photoCell post];
    
    PostAction *action = [PostAction new];
    action.postId = post.id;
    
    [BKIndicator showMessage:NSLocalizedString(@"add like...", @"提示")];
    [PostAction addLike:action
                callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                    if (error) {
                        [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
                        return;
                    }
                    [BKIndicator showMessageAndFadeOut:NSLocalizedString(@"success", @"提示") completion:nil];
                }];
    
}
- (void)photoTableViewCellDidClickedFavButton:(id)photoCell{
    Post *post = [photoCell post];
    
    PostAction *action = [PostAction new];
    action.postId = post.id;
    
    [BKIndicator showMessage:NSLocalizedString(@"add favorite...", @"提示")];
    [PostAction addFav:action
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                   if (error) {
                       [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
                       return;
                   }
                   [BKIndicator showMessageAndFadeOut:NSLocalizedString(@"success", @"提示") completion:nil];
               }];
    
}
- (void)photoTableViewCellDidClickedShareButton:(id)photoCell{
    Post *post = [photoCell post];
    PostAction *action = [PostAction new];
    action.postId = post.id;
    
    [PostAction addShare:action
                 callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                 }];
}
- (void)photoTableViewCellDidClickedCommentButton:(id)photoCell{
    Post *post = [photoCell post];
    PostDetailViewController *vc = [[PostDetailViewController alloc] init];
    vc.post = post;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)photoTableViewCellDidClickedAddCommentButton:(id)photoCell{
    Post *post = [photoCell post];
    self.commentView.tag = post.id;
    [self.commentView show];
}
#pragma BKCommentViewDelegate
- (void)commentView:(BKCommentView*)view didClickedSendButton:(id)button{
    NSString *content = [view.textView.text?:@"" stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (!content || content.length == 0) {
        [BKIndicator showMessageAndFadeOut:NSLocalizedString(@"content should not be null", @"提示") completion:nil];
        return;
    }
    Comment *comment = [Comment new];
    comment.content = content;
    comment.postId = view.tag;
    [BKIndicator showMessage:NSLocalizedString(@"submiting", @"提示")];
    [Comment add:comment
        callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
            if (error) {
                [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
                return;
            }
            view.textView.text = nil;
            [BKIndicator showMessageAndFadeOut:NSLocalizedString(@"submit success", @"提示") completion:nil];
        }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
